package com.zd.commodity.productMng.action;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.zd.commodity.action.BaseAction;
import com.zd.commodity.categoryMng.biz.ICategoryBiz;
import com.zd.commodity.categoryMng.entity.Category;
import com.zd.commodity.dto.ProductAttrExcel;
import com.zd.commodity.productMng.biz.IProductSeriesBiz;
import com.zd.commodity.productMng.entity.ProductAttr;
import com.zd.commodity.productMng.entity.ProductSeries;
import com.zd.commodity.userMng.entity.User;

@Scope("prototype")
@Controller("productSeriesAction")
public class ProductSeriesAction extends BaseAction<ProductSeries> {

	private IProductSeriesBiz productSeriesBiz;
	private String productPicIds;
	//将接收到的图片id字符串转成long数组
	private long[] getPicIds(){
		String[] split = productPicIds.replace(" ", "").split(",");
		long[] picIds = new long[split.length];
		for (int i=0;i<split.length;i++) {
			picIds[i]=Long.parseLong(split[i]);
		}
		return picIds;
	}
	
	//添加商品系列
	public String addProductSeries(){
		long[] picIds = getPicIds();
		User user = (User)request.getSession().getAttribute("user");
		super.getT().setCreator(user.getUsername());
		super.getT().setCreationTime(new Timestamp(System.currentTimeMillis()));
		productSeriesBiz.addProductSeries(super.getT(), picIds);
		return "actionList";
	}	
	//修改商品系列
	public String updateProductSeries(){
		long[] picIds = getPicIds();
		User user = (User)request.getSession().getAttribute("user");
		super.getT().setUpdater(user.getUsername());
		super.getT().setUpdateTime(new Timestamp(System.currentTimeMillis()));
		productSeriesBiz.updateProductSeries(super.getT(), picIds);
		return "actionList";
	}
	@Autowired
	public void setProductSeriesBiz(IProductSeriesBiz productSeriesBiz) {
		this.productSeriesBiz = productSeriesBiz;
	    this.setBaseBiz(productSeriesBiz);
	}
	
	@Autowired
	private ICategoryBiz categoryBiz;
	
	
	public void excelExport() throws Exception{
		ProductSeries productSeries=super.getT1();
		
		Map<String, String> map = new HashMap<String, String>();
		map.put("name", productSeries.getName());
		map.put("categoryName", productSeries.getCategory().getName());
		
		List list = productSeriesBiz.getExcelExportListBySql(ProductAttrExcel.class,"productSeriesExcelExport",map.size()>0?map:null, -1, -1);
		
		String[] title = new String[]{"系列名称","四级类别名称","创建时间","创建人"};
		String[] neededFields = new String[]{"name","categoryName","creationTime","creator"};
		super.excelExport("商品系列管理", list,title,neededFields);
	}
	
	public String updateCheck() throws Exception{
		String state = request.getParameter("state");
		ProductSeries productSeries = productSeriesBiz.get(super.getId());
		productSeries.setCheckSpec(Integer.valueOf(state));
		productSeriesBiz.update(productSeries);
		return listByPage();
	}
	
	
	
	public String getProductPicIds() {
		return productPicIds;
	}
	public void setProductPicIds(String productPicIds) {
		this.productPicIds = productPicIds;
	}
	
}
